package com.yami.shop.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.dto.UserTreeDto;
import com.yami.shop.bean.model.Order;
import com.yami.shop.bean.model.User;
import com.yami.shop.bean.param.OrderParamPublicParam;
import com.yami.shop.bean.vo.GetUserInfoVo;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.admin.util.SecurityUtils;
import com.yami.shop.service.OrderService;
import com.yami.shop.service.UserService;
import jodd.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/branchOperate/branchOperate")
public class BranchOperateController {
    @Autowired
    private UserService userService;
    @Autowired
    private OrderService orderService;

    /**
     * 获取分部下所有用户的树形结构
     * @param vipLevel3User
     * @return
     */
    @PreAuthorize("@pms.hasPermission('admin:user:page')")
    @GetMapping("/getVipLevel3UserLowerLevel")
    public ServerResponseEntity<List<UserTreeDto>> getOrderStatistics(String vipLevel3User) {
        if(StringUtil.isBlank(vipLevel3User)){
            return ServerResponseEntity.showFailMsg("参数 vipLevel3User 不能为空");
        }
        List<User> lowerLevel = userService.getLowerLevel(vipLevel3User, new ArrayList<>());

        lowerLevel.add(userService.getById(vipLevel3User));//加上自己

        List<UserTreeDto> lsit1=new ArrayList<>();

        for(User user1:lowerLevel){
            String vipLevelName=null;
            if (user1.getVipLevel() != null){
                if(user1.getVipLevel() == 0){
                    vipLevelName = "普通会员";
                }else if(user1.getVipLevel() == 1){
                    vipLevelName = "vip";
                }else if(user1.getVipLevel() == 2){
                    vipLevelName = "svip";
                }else if(user1.getVipLevel() == 3){
                    vipLevelName = "分部";
                }
            }
            UserTreeDto dto=new UserTreeDto();
            dto.setId(user1.getUserId());
            dto.setPid(user1.getHigherLevelUid());
            dto.setName(user1.getNickName()+(vipLevelName!=null?"("+vipLevelName+")":""));
            dto.setImage_url(user1.getPic());

            lsit1.add(dto);
        }

        Map<String, List<UserTreeDto>> collect2 = lsit1.stream().filter((m) -> !StringUtil.equals(m.getId(), vipLevel3User)).collect(Collectors.groupingBy(m -> m.getPid()));
        lsit1.forEach(m -> m.setChildren(collect2.get(m.getId())));


        List<UserTreeDto> collect3 = lsit1.stream().filter((m) -> StringUtil.equals(m.getId(), vipLevel3User)).collect(Collectors.toList());
        return ServerResponseEntity.success(collect3);
    }

    /**
     * 查看用户信息
     * @param uid
     * @return
     */
    @PreAuthorize("@pms.hasPermission('admin:user:page')")
    @GetMapping("/getUserInfo")
    public ServerResponseEntity<GetUserInfoVo> getUserInfo(String uid) {
        if(StringUtil.isBlank(uid)){
            return ServerResponseEntity.showFailMsg("参数 uid 不能为空");
        }

        User byId = userService.getById(uid);
        if(byId == null){
            return ServerResponseEntity.showFailMsg("查询结果为空");
        }

        GetUserInfoVo vo=new  GetUserInfoVo();
        BeanUtil.copyProperties(byId,vo);


        vo.setLoginPassword("");
        vo.setPayPassword("");

        if(StringUtil.isNotBlank(vo.getReward2YuanUid())){
            User byId1 = userService.getById(vo.getReward2YuanUid());
            if(byId1 != null){
                vo.setReward2YuanNickName(byId1.getNickName());
            }
        }
        return ServerResponseEntity.success(vo);
    }

    /**
     * 用户订单信息
     * @param
     * @return
     */
    @PreAuthorize("@pms.hasPermission('admin:user:page')")
    @GetMapping("/getUserOrderInfo")
    public ServerResponseEntity<IPage<Order>> getUserOrderInfo(OrderParamPublicParam orderParam, PageParam<Order> page) {
        if(StringUtil.isBlank(orderParam.getUserId())){
            return ServerResponseEntity.showFailMsg("参数 userId 不能为空");
        }
        Long shopId = SecurityUtils.getSysUser().getShopId();
        orderParam.setShopId(shopId);
        IPage<Order> orderPage = orderService.pageOrdersDetailByOrderPublicParam(page, orderParam);
        return ServerResponseEntity.success(orderPage);
    }

}
